import Cookies from 'js-cookie'
import { setIMProfile } from '@/utils/storage'
/** 
 * 设置关联关系
 * sid: 自己的id
 * tid: 对方的id
 */

export const setAssociated = () => {
  if (process.env.CHAT_TEST) {
    toSetIMProfile(`${process.env.CHAT_TEST_USERID}`, `${process.env.CHAT_TEST_FRIENDID}`)
    return
  }
  console.log('进入了：', "参数阶段");
  const arr = getIdsFromHref()
  if (arr.length === 2) {
    console.log(arr[0], arr[1], '最终参数');
    toSetIMProfile(arr[0], arr[1])
  }
}

// 从url中获取sid和tid
export const getIdsFromHref = () => {
  const href = window.location.href
  let index = href.lastIndexOf('/');
  let lastSegment = href.substring(index + 1);
  if (lastSegment.includes('.html')) {
    lastSegment = lastSegment.replace('.html', '')
  }
  const arr = lastSegment.split('_')
  return arr
}

const toSetIMProfile = (sid: string, tid: string) => {
  const imToken = Cookies.get(`feijiuImUserToken_${sid}`)
  setIMProfile({ imToken, userID: sid, friendID: tid })
}
